package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.model.Record;
import com.j48.entrancegurad.model.User;
import com.j48.entrancegurad.service.IHostService;
import com.j48.entrancegurad.service.IRecordService;
import com.j48.entrancegurad.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class RecordController {

    @Autowired
    private IRecordService recordService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHostService hostService;

    /**
     * 门禁记录列表
     * @param request
     * @return
     */
    @RequestMapping("/recordList")
    @ResponseBody
    public ModelAndView recordList(HttpServletRequest request){
        ModelAndView model=null;
        List<Record> list = new ArrayList<Record>();
        list=recordService.selectAll();
        if(list!=null) {
            List<Integer> user_idList = new ArrayList<Integer>();
            for (Record r : list) {
                user_idList.add(r.getRecord_user_id());
            }
            List<User> userlist = new ArrayList<User>();
            for (int i : user_idList) {
                User user = new User();
                user.setUser_id(i);
                user = userService.selectByUser_id(user);
                userlist.add(user);
            }
            List<Host> recordhostList=new ArrayList<Host>();
            for(User u:userlist){
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                recordhostList.add(host);
            }
            request.getSession().setAttribute("recordList", list);
            request.getSession().setAttribute("recordUserList", userlist);
            request.getSession().setAttribute("recordHostList", recordhostList);
        }
        model = new ModelAndView("jsp/record");
        return model;
    }

    /**
     * 按用户来查询门禁记录
     * @param record_user_name
     * @param request
     * @return
     */
    @RequestMapping("/recordListByUser")
    @ResponseBody
    public ModelAndView recordListByUser(String record_user_name,HttpServletRequest request){
        if(null==record_user_name||"".equals(record_user_name)){
            return recordList(request);
        }else{
            ModelAndView model = null;
            User user1 = new User();
            user1.setUser_name(record_user_name);
            List<User> userlist = userService.selectByUser_name(user1);
            List<User> userList=new ArrayList<User>();
            List<Record> list = new ArrayList<Record>();
            List<Host> hosts=new ArrayList<Host>();
            if(userlist!=null) {
                for (User user:userlist) {
                    Record record = new Record();
                    record.setRecord_user_id(user.getUser_id());
                    List<Record> list1 = new ArrayList<Record>();
                    list1 = recordService.selectByUser(record);
                    if(list1!=null){
                        for (Record r:list1) {
                            int i=r.getRecord_user_id();
                            User user2=new User();
                            user2.setUser_id(i);
                            user2=userService.selectByUser_id(user2);
                            userList.add(user2);
                            list.add(r);
                            Host host1=new Host();
                            host1.setHost_id(user2.getUser_host_id());
                            host1=hostService.selectById(host1);
                            hosts.add(host1);
                        }
                    }
                }
            }
            for (Record r:list) {
                System.out.println(r.getRecord_user_id()+"--"+r.getRecord_passtime());
            }
            model = new ModelAndView("jsp/recordSearchByUser");
            request.getSession().setAttribute("recordList", list);
            request.getSession().setAttribute("recordUser", userList);
            request.getSession().setAttribute("recordHost", hosts);
            return model;
        }
    }

    /**
     * 按日期查询门禁记录
     * @param record_passtime
     * @param request
     * @return
     */
    @RequestMapping("/recordListByDate")
    @ResponseBody
    public ModelAndView recordListByDate(String record_passtime, HttpServletRequest request){
        if(null==record_passtime||"".equals(record_passtime)){
            return recordList(request);
        }else {
            ModelAndView model = null;
            List<Record> list = new ArrayList<Record>();
            Timestamp ts = null;
            record_passtime += " 00:00:00";
            try {
                ts = Timestamp.valueOf(record_passtime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Record record = new Record();
            record.setRecord_passtime(ts);
            list = recordService.selectByDate(record);
            List<Integer> user_idList = new ArrayList<Integer>();
            List<User> userlist = new ArrayList<User>();
            if(list!=null) {
                for (Record r : list) {
                    user_idList.add(r.getRecord_user_id());
                }
                for (int i : user_idList) {
                    User user = new User();
                    user.setUser_id(i);
                    user = userService.selectByUser_id(user);
                    userlist.add(user);
                }
            }
            List<Host> recordhostList=new ArrayList<Host>();
            for(User u:userlist){
                Host host=new Host();
                host.setHost_id(u.getUser_host_id());
                host=hostService.selectById(host);
                recordhostList.add(host);
            }
            request.getSession().setAttribute("recordList", list);
            request.getSession().setAttribute("recordUserList", userlist);
            request.getSession().setAttribute("recordHostList", recordhostList);
            model = new ModelAndView("jsp/record");
            return model;
        }
    }
}
